<?php
/**
 * Created by PhpStorm.
 * Content: 阿里云云市场api接口
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/11/20
 * Time: 12:00
 */


////////////////////// 阿里云云市场api接口的Appcode

/**
 * APPCODE
 *
 * 阿里云云市场api接口
 *
 * @return string
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function alicloudapi_appcode(){
    return env('ALICLOUDAPI_APPCODE','');
}


////////////////////// 行驶证识别

/**
 * 天眼识别行驶证
 *
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function ocr_vehicle_for_tianyan($s_img){
    $host = "http://vehicleocr.shumaidata.com";
    $path = "/vehicleocr";
    $appcode = alicloudapi_appcode();
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    //根据API的要求，定义相对应的Content-Type
    //    array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
    $url = $host . $path;
    $arr_data = [
      'image'=>$s_img
    ];

    $res = curl_request_post($url,$arr_data,['header'=>$headers]);
    $result = json_decode($res,true);
    if($result['success'] === true && $result['code'] == 200){
        $arr_data = $result['data']['result'];
        $arr_data['brand_model'] = $arr_data['model'];
        $arr_data['issue_date'] = str_replace('-','',$arr_data['issue_date']);
        $arr_data['register_date'] = str_replace('-','',$arr_data['register_date']);
        return  hello_success('成功',$arr_data);
    }
    else{
        if(empty($res)){
            return hello_warning('接口暂未开通',999);

        }

        return hello_warning($result['msg'],$result['code']);
    }
}


////////////////////// 快递查询

/**
 * 四川涪擎大数据-快递查询
 *
 * 阿里云市场（全国快递物流查询-快递查询接口） 四川涪擎大数据技术有限公司
 *
 * @param string $s_no
 * @param string $s_company_type
 * @param array  $arr_option
 *
 * @return bool|string
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function kuaidi_search($s_no = '',$s_company_type = '',$arr_option = []){
    $host = "https://wuliu.market.alicloudapi.com";//api访问链接
    $path = "/kdi";//API访问后缀
    $appcode = alicloudapi_appcode();//替换成自己的阿里云appcode
//    dd($appcode);
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    //    if(strtoupper($s_company_type) == 'SFEXPRESS'){
    //        if($s_phone == ''){
    //            return json_encode(['status'=>999,'msg'=>'顺丰快递需要辅助手机号查询']);
    //        }
    //        $s_no = $s_no.':'.$s_phone;
    //    }

    $querys = "no=".$s_no."&type=".$s_company_type;  //参数写在这里
    $url = $host . $path . "?" . $querys;//url拼接
    $result = curl_get($url,['header'=>$headers]);
    return $result;
}


////////////////////// 车型大全

/**
 * 极速数据-接口域名
 *
 * 阿里云市场（极速数据-车型大全接口）
 *
 * @return string
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function jisu_host(){
    return "https://jisucxdq.market.alicloudapi.com";
}

/**
 * 极速数据-车型大全-获取所有品牌接口
 *
 * 阿里云市场（极速数据-车型大全接口）
 *
 * @return bool|string
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function jisu_car_brand(){
    $host = jisu_host();
    $path = "/car/brand";
    $appcode = alicloudapi_appcode();
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $url = $host . $path;
    $result = curl_get($url,['header'=>$headers]);
    return $result;
}

/**
 * 极速数据-车型大全-根据品牌获取所有车型接口
 *
 * 阿里云市场（极速数据-车型大全接口）
 *
 * @return bool|string
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function jisu_car_carlist($n_pid){
    $host = jisu_host();
    $path = "/car/carlist";
    $appcode = alicloudapi_appcode();
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "parentid=".$n_pid;
    $url = $host . $path . "?" . $querys;
    $result = curl_get($url,['header'=>$headers]);
    return $result;

}

/**
 * 极速数据-车型大全-根据ID获取车型详情接口
 *
 * 阿里云市场（极速数据-车型大全接口）
 *
 * @return bool|string
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function jisu_car_detail($n_carid){
    $host = jisu_host();
    $path = "/car/detail";
    $appcode = alicloudapi_appcode();
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "carid=".$n_carid;
    $url = $host . $path . "?" . $querys;
    $result = curl_get($url,['header'=>$headers]);
    return $result;
}

/**
 * 品牌获取所有车型树形数组转为列表数组
 *
 * @param $arr_model_tree
 *
 * @return array
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function vehicle_model_tree_to_list($arr_model_tree){
    $arr_list = [];
    if(isset($arr_model_tree)){
        foreach ($arr_model_tree as $value)
        {
            $arr_car = $value;
            unset($arr_car['carlist']);
            $arr_car['logo'] = '';
            $arr_car['salestate'] = '';
            $arr_car['yeartype'] = '';
            $arr_car['productionstate'] = '';
            $arr_car['sizetype'] = '';
            $arr_list[] = $arr_car;
            if(isset($value['carlist'])){
                foreach ($value['carlist'] as $vv)
                {
                    $arr_car = $vv;
                    unset($arr_car['list']);
                    $arr_car['yeartype'] = '';
                    $arr_car['productionstate'] = '';
                    $arr_car['sizetype'] = '';
                    $arr_list[] = $arr_car;
                    if(isset($vv['list'])){
                        foreach ($vv['list'] as $vaa)
                        {
                            $arr_car = $vaa;
                            $arr_car['fullname'] = '';
                            $arr_list[] = $arr_car;
                        }
                    }

                }
            }
        }
    }
    return $arr_list;
}

/**
 * 创建车型大全表
 *
 * @param $s_full_table
 *
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function create_vehicle_model_table($s_full_table){
    $s_sql = "DROP TABLE IF EXISTS ".$s_full_table;
    DB::statement($s_sql);
    $s_sql = "CREATE TABLE `jinchen_car_test`.`".$s_full_table."`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '名称',
  `fullname` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '全名',
  `initial` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '首字母',
  `parentid` int(11) NOT NULL DEFAULT 0 COMMENT '上级id',
  `logo` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'logo',
  `depth` tinyint(4) NOT NULL DEFAULT 100 COMMENT '深度 1品牌 2子公司 3车型 4具体车型',
  `salestate` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '销售状态',
  `price` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '官方指导价',
  `yeartype` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '年款',
  `productionstate` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '生产状态',
  `sizetype` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '尺寸类型',
  `created_at` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
  `updated_at` timestamp(0) NULL DEFAULT NULL,
  `deleted_at` timestamp(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '车型大全表' ROW_FORMAT = Dynamic";
    DB::statement($s_sql);
}

/*更新车型大全数据*/
function refresh_vehicle_model(){
    try {
        $s_prefix = DB::getConfig('prefix');
        $s_table = 'vehicle_model';
        $s_full_table = $s_prefix.$s_table;

        //新建车型大全表
        create_vehicle_model_table($s_full_table);
        dd(9);

        //接口查询数据 同时存入数据库
        $res = jisu_car_brand();
        $result = json_decode($res,true);
        if($result['status'] != 0){
            return hello_error($result['msg']);
        }

        $arr_car_brand = $result['result'];
        foreach ($arr_car_brand as $value)
        {
            $res = jisu_car_carlist($value['id']);
            $result = json_decode($res,true);
            $value['salestate'] = '';
            $value['yeartype'] = '';
            $value['productionstate'] = '';
            $value['sizetype'] = '';
            $value['fullname'] = '';
            $value['price'] = 0;

            if($result['status'] != 0){
                //                return hello_error($result['msg']);
                $arr_car_list = [];
                $arr_car_list[] = $value;

            }
            else
            {

                $arr_car_list = vehicle_model_tree_to_list($result['result']);
                $arr_car_list[] = $value;
            }

            //            \HiCommon\MongodbModel\MongoTest::insert($arr_car_list);
            $res = \HiCommon\Model\CarModel::insert($arr_car_list);
            if($res === false){
                return hello_error('添加失败');
            }
            sleep(2);
        }

        return  hello_success('添加成功');

    }
    catch (\Exception $exception) {
        return hello_error('失败',[],$exception->getMessage());
    }

}

?>